<template>
  <a-modal
    title="新增优惠"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="类别"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            placeholder="请选择类别"
            v-decorator="[
              'type',
              {rules: [{ required: true, message: '请选择类别'}]}
            ]" >
            <a-select-option value="1" >优惠金额</a-select-option>
            <a-select-option value="2" >赠送期限</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="优惠内容"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['content', {rules: [{required: true, message: '请输入优惠内容'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveDiscount } from '@/api/manage'
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,

      form: this.$form.createForm(this),
      type: 1,
      token: '',
      ar_id: '',
      cm_mg_id: '',
      thisId: 0
    }
  },
  methods: {
    go (token, ar_id, cm_mg_id) {
      this.visible = true
      this.token = token
      this.ar_id = ar_id
      this.cm_mg_id = cm_mg_id
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values.pms_ar_id = this.ar_id
          values.cm_message_id = this.cm_mg_id
          const savedate = {
            token: this.token, ar_id: this.ar_id, cm_mg_id: this.cm_mg_id, saveData: values
          }
          saveDiscount(savedate).then(res => {
            this.visible = false
            this.confirmLoading = false
            this.$emit('ok', values)
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
